package com.leetcode.partition3;

import java.util.Arrays;

/**
 * @author `RKC`
 * @date 2021/8/23 10:34
 */
public class LC213打家劫舍2 {

    public static int rob(int[] nums) {
        if (nums.length == 0) return 0;
        if (nums.length == 1) return nums[0];
        if (nums.length == 2) return Math.max(nums[0], nums[1]);
        //考虑首元素，不考虑尾元素。考虑尾元素，不考虑首元素
        return Math.max(dynamicProgramming(nums, 0, nums.length - 1), dynamicProgramming(nums, 1, nums.length));
    }

    public static void main(String[] args) {
//        int[] nums = { 1, 2, 3, 1 };
        int[] nums = { 0, 0 };
        System.out.println(rob(nums));
    }

    private static int dynamicProgramming(int[] nums, int start, int end) {
        if (end == start) return nums[start];
        int[] dp = new int[nums.length];
        dp[start] = nums[start];
        dp[start + 1] = Math.max(nums[start], nums[start + 1]);
        for (int i = start + 2; i < end; i++) {
            dp[i] = Math.max(dp[i - 2] + nums[i], dp[i - 1]);
        }
        System.out.println(Arrays.toString(dp));
        return dp[end - 1];
    }
}
